<?php

class ProfileController extends Zend_Controller_Action 
{
    function init()
    {
        $this->initView();
        Zend_Loader::loadClass('AuxDbTable');
        Zend_Loader::loadClass('User');
        Zend_Loader::loadClass('Startpage');
        Zend_Loader::loadClass('Mainmenu');
        $this->view->baseUrl = $this->_request->getBaseUrl();
        $this->view->user = Zend_Auth::getInstance()->getIdentity();
    }
    
    function preDispatch()
    {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('auth/login');
        }
        $menu = new Mainmenu();
        $this->view->main_menu = $menu->getMenu($this->view->user->id);
    }

    function indexAction()
    {
        $this->view->title = "Users";
        $this->view->content_title = "Users";
        $obj = new User();
        
        $this->view->content_title = $this->view->title = "Edit My Profile";
        
        if (strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
            Zend_Loader::loadClass('Zend_Filter_StripTags');
            $filter = new Zend_Filter_StripTags();
            
            $id = $this->view->user->id;
            $password = trim($filter->filter($this->_request->getPost('password')));
            $email = trim($filter->filter($this->_request->getPost('email')));
            $first_page = trim($filter->filter($this->_request->getPost('first_page')));
            //$birthday = trim($filter->filter($this->_request->getPost('birthday')));
            
            if ($id !== false) {
                $this->view->message = '';
                if($this->validate($id,$password,$email)) {
                    $data = array(
                        'password' => $password,
                        'email' => $email,
                        'first_page'  => $first_page/*,
                        'birthday' => $birthday?$birthday:null*/
                    );
                    $where = 'id = ' . $id;
                    $obj->update($data, $where);
                
                    $this->_redirect('/profile');
                    return;
                } else {
                    $this->view->f_user = $obj->fetchRow('id='.$id);
                    
                    $this->view->content_title = $this->view->f_user->first_name.' '.$this->view->f_user->last_name.' : My Profile';
                    
                    if(isset($password)) $this->view->f_user->password = $password;
                    if(isset($email)) $this->view->f_user->email = $email;
                    if(isset($first_page)) $this->view->f_user->first_page = $first_page;
                    //if(isset($birthday)) $this->view->f_user->birthday = $birthday;
                }
            }
        } else {
            $id = $this->view->user->id;
            if ($id > 0) {
                $this->view->f_user = $obj->fetchRow('id='.$id);
                $this->view->content_title = $this->view->f_user->first_name.' '.$this->view->f_user->last_name.' : My Profile';
                
            }
        }

        // additional view fields required by form
        $this->view->action = 'edit';
        $this->view->buttonText = 'Update';
        
        $this->render();
    } 
    
    function validate($id,$password,$email/*,$birthday*/)
    {
        $valid = true;
        $obj = new User();
        
        if(strlen($password)==0)
        {
            $this->view->message .= 'Enter Password please.<br>';
            $valid = false;
        }
        //email
        
        $email_pattern = '/^[A-Z0-9\._\-]+@[A-Z0-9\.\-]+\.[A-Z]{2,6}$/i';
        if(strlen($email)==0)
        {
            $this->view->message .= 'Enter Email please.<br>';
            $valid = false;
        }
        elseif(!preg_match($email_pattern,$email))
        {
            $this->view->message .= 'Invalid Email '.$email.'.<br>';
            $valid = false;
        } 
        
        //date 
        $date_pattern = '/^[0-9]{4}\-(?:0[1-9]|1[0-2])\-(?:[0-2]?[0-9]|3[0-1])$/';
        /*if(strlen($birthday)>0)
        {
            if(!preg_match($date_pattern,$birthday))
            {
                $this->view->message .= 'Invalid Birth Date '.$birthday.'. Format yyyy-mm-dd.<br>';
                $valid = false;
            } 
        }*/
        return $valid;
    }
    
}